package com.site.codegen.utils;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Properties;

public class DatabaseUtil {

    public static Connection getConnection(String driverClassName, String connectionUrl, String username, String password, String driverPath) throws Exception {
        File jarFile = new File(driverPath);
        URL url = jarFile.toURI().toURL();
        URL[] urls = new URL[]{jarFile.toURI().toURL()};
        URLClassLoader classLoader = new URLClassLoader(urls);
        Class<?> driverClass = classLoader.loadClass(driverClassName);
        Driver driver = (Driver) driverClass.getConstructor().newInstance();

        Properties connectionParam = new Properties();
        connectionParam.put("user", username);
        connectionParam.put("password", password);
        return driver.connect(connectionUrl, connectionParam);
    }
}
